package com.itcast.clientx.controller;

import com.alibaba.fastjson.JSONObject;
import com.itcast.clientx.bean.Maven;
import com.itcast.clientx.mapper.MavenMapper;
import com.itcast.clientx.service.RepositoryService;
import com.itcast.clientx.util.Client;
import com.itcast.clientx.util.Crawler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;

@RestController
public class RepositoryController {
    @Autowired
    private Crawler crawler;
    @Autowired
    private MavenMapper mapper;
    @Autowired
    private Client client;
    /**
     * http://127.0.0.1:8084/index/smart-admin-service-parent
     * @param content
     * @return
     */
    @GetMapping("/index/{content}")
    public String index(@PathVariable("content") String content){
        crawler.test(content);
        return "{25:25}";
    }

    @GetMapping("/cookie")
    public String cookie(HttpServletResponse response){
        Cookie username = new Cookie("username","liusu");
        Cookie password = new Cookie("password","123456");
        username.setPath("/cookie");
        username.setMaxAge(3600);
        password.setPath("/analysis");
        JSONObject json = new JSONObject();
        json.put("msg","----");
        response.addCookie(username);
        response.addCookie(password);
        return json.toString();
    }



    /**
     * MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImJiODEyNDUwLWJjZmUtMTFlYi04YzNlLTdiYWEyM2NlNjU3MSJ9LCJleHAiOjE2NTM0NjgyNjAsIm5iZiI6MTYyMTkzMjI2MCwiaWF0IjoxNjIxOTMyMjYwfQ.5vB0nX5EzLUjwRVUGrJALr__AM_XGJyi0X97URep7oc
     * MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImJiODEyNDUwLWJjZmUtMTFlYi04YzNlLTdiYWEyM2NlNjU3MSJ9LCJleHAiOjE2NTM0NjgzNzQsIm5iZiI6MTYyMTkzMjM3NCwiaWF0IjoxNjIxOTMyMzc0fQ.rZ9PwVQLxSUI8JWDlVtmGpx_50Os4EG78UxtP2FneMc
     * @param request
     * @return
     */
    @GetMapping("/analysis")
    public String analysiscookie(HttpServletRequest request){

        JSONObject json = new JSONObject();
        json.put("msg","----");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName()+"="+cookie.getValue());
        }
        Maven mvn = new Maven();
        mvn.setRepositoryid(84);
        mvn.setVersion("1.0.3");
        mvn.setRepository("----");
        mvn.setSpringboot(0);
        mvn.setUsages(0);
        mvn.setPublishtime(new Date());

        try {
            mapper.insert(mvn);
        }catch (Exception exception){
            System.out.println(exception.getMessage());
            exception.printStackTrace();
        }finally {

        }
        return json.toString();
    }

    /**
     * _ga=GA1.2.104143695.1620453605; __gads=ID=b38aaa7b11b51a40-2280bbe9e0c700fc:T=1620455551:RT=1620455551:S=ALNI_MaCwLXxXyg0DwHiTyJ692bfuXLMDQ; __cfduid=d6824abadd7573d53b093425292f04aa41620546458; _gid=GA1.2.705246280.1621669208; cf_chl_prog=a9; cf_clearance=40e4a5a3cceb675d5ae22c48c457013ea36e1760-1621987901-0-250; __cf_bm=71415ff57aa6dcebd38b3db4162aaa4367c45444-1621987902-1800-AQUCydofs2CYY+JwVSHMNSIovO60JqElt447Oy4rew/x+axhgGt+t8PDKjvYEP6umo25je5EzKD8so4A6tHSrEPd9ReqU6mm7OFRfQAHeICxEi0d3j/St7FlaTgj0PKTBA==; MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImM0NmVmNDExLWJkYjgtMTFlYi04YjUyLTY3NDY3MzIxZmE1OSJ9LCJleHAiOjE2NTM1MjQ2ODMsIm5iZiI6MTYyMTk4ODY4MywiaWF0IjoxNjIxOTg4NjgzfQ.4nALSLN34fOx2vKY6i0RGfnS8VDaqQ6zEbio6D9pHJA
     * _ga=GA1.2.104143695.1620453605; __gads=ID=b38aaa7b11b51a40-2280bbe9e0c700fc:T=1620455551:RT=1620455551:S=ALNI_MaCwLXxXyg0DwHiTyJ692bfuXLMDQ; __cfduid=d6824abadd7573d53b093425292f04aa41620546458; _gid=GA1.2.705246280.1621669208; cf_chl_prog=a9; cf_clearance=40e4a5a3cceb675d5ae22c48c457013ea36e1760-1621987901-0-250; __cf_bm=71415ff57aa6dcebd38b3db4162aaa4367c45444-1621987902-1800-AQUCydofs2CYY+JwVSHMNSIovO60JqElt447Oy4rew/x+axhgGt+t8PDKjvYEP6umo25je5EzKD8so4A6tHSrEPd9ReqU6mm7OFRfQAHeICxEi0d3j/St7FlaTgj0PKTBA==; MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImM0NmVmNDExLWJkYjgtMTFlYi04YjUyLTY3NDY3MzIxZmE1OSJ9LCJleHAiOjE2NTM1MjQ3NjQsIm5iZiI6MTYyMTk4ODc2NCwiaWF0IjoxNjIxOTg4NzY0fQ.x6fy1npN58Aufw_ZS9dqO1znLgt0Xao3gHE-oGPIXiw
     *
     * MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImM0NmVmNDExLWJkYjgtMTFlYi04YjUyLTY3NDY3MzIxZmE1OSJ9LCJleHAiOjE2NTM1MjQ3NjQsIm5iZiI6MTYyMTk4ODc2NCwiaWF0IjoxNjIxOTg4NzY0fQ.x6fy1npN58Aufw_ZS9dqO1znLgt0Xao3gHE-oGPIXiw
     * MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6ImM0NmVmNDExLWJkYjgtMTFlYi04YjUyLTY3NDY3MzIxZmE1OSJ9LCJleHAiOjE2NTM1MjQ4ODgsIm5iZiI6MTYyMTk4ODg4OCwiaWF0IjoxNjIxOTg4ODg4fQ.bEt8keBeYje92r1lypkbLy3zA6aqbi0SeDaJ2FU7l94
     *
     *
     *
     *
     *
     * @param request
     * @return
     */
    @GetMapping("/search")
    public String search(HttpServletRequest request){

        JSONObject json = new JSONObject();
        json.put("msg","----");
        String cookie = "_ga=GA1.2.104143695.1620453605; __gads=ID=b38aaa7b11b51a40-2280bbe9e0c700fc:T=1620455551:RT=1620455551:S=ALNI_MaCwLXxXyg0DwHiTyJ692bfuXLMDQ; __cfduid=d6824abadd7573d53b093425292f04aa41620546458; _gid=GA1.2.705246280.1621669208; cf_chl_prog=a9; cf_clearance=40e4a5a3cceb675d5ae22c48c457013ea36e1760-1621987901-0-250; __cf_bm=71415ff57aa6dcebd38b3db4162aaa4367c45444-1621987902-1800-AQUCydofs2CYY+JwVSHMNSIovO60JqElt447Oy4rew/x+axhgGt+t8PDKjvYEP6umo25je5EzKD8so4A6tHSrEPd9ReqU6mm7OFRfQAHeICxEi0d3j/St7FlaTgj0PKTBA==; MVN_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6Ijk2OTIwZTEwLWJkMzYtMTFlYi1hNjIwLTQ5MDY1ZTNjNGRkMSJ9LCJleHAiOjE2NTM1MjQwMTgsIm5iZiI6MTYyMTk4ODAxOCwiaWF0IjoxNjIxOTg4MDE4fQ.ZLuDqxesOd18Rljxskvr7IhfC-9tJOtd9KFun5wT8d8";



        String html = client.getHTML("https://mvnrepository.com/search?q=text",cookie);
        System.out.println(html);
        return json.toString();
    }
}
